package com.adong.web;

import com.adong.pojo.Cart;
import com.adong.pojo.User;
import com.adong.service.OrderService;
import com.adong.service.impl.OrderServiceImpl;
import com.adong.utils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author adong
 */
public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到购物车cart对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //获取userid
        User user = (User) req.getSession().getAttribute("user");
        Integer userId = user.getId();
        //调用orderService.createOrder(Cart,UserId)生成订单
        String orderId = orderService.createOrder(cart, userId);
        req.getSession().setAttribute("orderId", orderId);
        cart.clear();//结账清空购物车
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");

        // req.setAttribute("orderId", orderId);
        //req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);


    }
}
